package com.ktwlrj.securitycenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktwlrj.core.constant.ResultCode;
import com.ktwlrj.core.exception.ActionException;
import com.ktwlrj.core.util.convert.ConvertUtil;
import com.ktwlrj.securitycenter.controller.args.SafeInterceptTargetArgs;
import com.ktwlrj.securitycenter.dto.SafeInterceptTargetDto;
import com.ktwlrj.securitycenter.entity.SafeInterceptTargetEntity;
import com.ktwlrj.securitycenter.mapper.SafeInterceptTargetMapper;
import com.ktwlrj.securitycenter.service.SafeInterceptTargetService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 预警拦截对象业务层实现
 *
 * @author code generation
 * @date 2020-02-24 18:08:13
 * @since JDK1.8
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SafeInterceptTargetServiceImpl extends ServiceImpl<SafeInterceptTargetMapper, SafeInterceptTargetEntity> implements SafeInterceptTargetService {
    private final SafeInterceptTargetMapper mapper;


    @Override
    public void batchSave(List<SafeInterceptTargetArgs> args) throws RuntimeException {
        // 删除预警拦截id下所有对象
        mapper.deleteByBusiId(args.get(0).getInterceptId());
        // 新增预警拦截对象
        List<SafeInterceptTargetEntity> entityList = ConvertUtil.listAtoListB(args, SafeInterceptTargetEntity.class);
        entityList.forEach(v -> {
            v.setIdcardEncry(v.getIdcard());
        });
        log.debug("----------------entityList---------{}",entityList);
        boolean result = super.saveBatch(entityList);
        if (!result) {
            throw new ActionException(ResultCode.DATABASE_INSER_ERROR);
        }
    }


    @Override
    public List<SafeInterceptTargetDto> selectListByModel(String interceptId) throws RuntimeException {
        QueryWrapper<SafeInterceptTargetEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(SafeInterceptTargetEntity::getInterceptId, interceptId);
        List<SafeInterceptTargetEntity> entityList = mapper.selectList(queryWrapper);
        List<SafeInterceptTargetDto> interceptTargetDtoList = new ArrayList<>();
        if (entityList != null && entityList.size() > 0) {
            interceptTargetDtoList = ConvertUtil.listAtoListB(entityList, SafeInterceptTargetDto.class);
        }

        return interceptTargetDtoList;
    }
}